package com.tj.view.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.tj.view.R;
import com.tj.view.tool.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/8 0008.
 *
 * 首先了解一下ViewFlipper，http://blog.csdn.net/ff20081528/article/details/18089883
 *
 * https://github.com/sfsheng0322/MarqueeView
 *
 * 垂直跑马灯
 *
 * viewFlipper 是带有动画的切换view,继承Framelayout
 *
 * 当前MarqueeView中会添加TextView，所以这个自定义view的核心就是viewFlipper中带有滚动动画的切换textview的功能
 *
 */
public class MarqueeView extends ViewFlipper {

    private static final String TAG = "MarqueeView";
    private Context mContext;
    private int interval = 2*1000;//默认的翻页时间
    private int animDuration = 500;//一行文字动画时长
    private boolean isSetAnimation = false;//是否设置动画时间
    private boolean singleLine = false;
    private int textSize = 14;
    private int textColor = 0xffffffff;
    private static final int TEXT_GRAVITY_LEFT =0, TEXT_GRAVITY_CENTER=1, TEXT_GRAVITY_RIGHT=2;

    private List<String> notices;//数据源
    private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;

    private static final int VERTICAL = 0, HORIZONTAL = 1;
    private int oritetation = VERTICAL;
    private int animInId = R.anim.anim_marquee_top_in;
    private int animOutId = R.anim.anim_marquee_top_out;

    public MarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr){
        this.mContext = context;
        if(notices == null){
            notices = new ArrayList<>();
        }

        /**
         * 获取定义的属性
         */
        TypedArray typedArray = getContext()
                .obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
        //获取自定义属性mvInterval
        interval = typedArray
                .getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
        isSetAnimation = typedArray
                .hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
        singleLine =  typedArray
                .getBoolean(R.styleable.MarqueeViewStyle_mvSingleLine, false);
        animDuration = typedArray
                .getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
        //为何要判断一下？？
        if(typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)){
            textSize = (int)typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
            textSize = DisplayUtil.px2sp(mContext, textSize);
        }

        textColor = typedArray
                .getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
        int gravityInt = typedArray
                .getInt(R.styleable.MarqueeViewStyle_mvGravity, TEXT_GRAVITY_LEFT);
        switch (gravityInt){
            case TEXT_GRAVITY_LEFT:
                gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
                break;
            case TEXT_GRAVITY_CENTER:
                gravity = Gravity.CENTER;
                break;
            case TEXT_GRAVITY_RIGHT:
                gravity = Gravity.RIGHT|Gravity.CENTER_VERTICAL;
                break;
        }

        oritetation = typedArray
                .getInt(R.styleable.MarqueeViewStyle_mvAnimOritentation, VERTICAL);
        if(oritetation == VERTICAL){
            animInId = R.anim.anim_marquee_top_in;
            animOutId = R.anim.anim_marquee_top_out;
        }else if(oritetation == HORIZONTAL){
            animInId = R.anim.anim_marquee_right_in;
            animOutId = R.anim.anim_marquee_right_out;
        }

        typedArray.recycle();//必须要

        /**
         * 属性值，进行相应动能设置
         */
        //viewFlipper的切换间隔时间方法#使用了属性MarqueeViewStyle_mvInterval
        setFlipInterval(interval);

        Animation animIn = AnimationUtils.loadAnimation(mContext, animInId);
        //执行动画间隔
        if(isSetAnimation) animIn.setDuration(animDuration);
        //进入动画
        setInAnimation(animIn);

        //如果设置了动画间隔，执行跑马灯文字离开动画
        Animation animOut = AnimationUtils.loadAnimation(mContext, animOutId);
        if(isSetAnimation) animOut.setDuration(animDuration);
        //离开动画
        setOutAnimation(animOut);
    }

    public void startWithList(List<String> list){
        setNotices(list);
        start();
    }

    /**
     *  启动轮转
     */
    public boolean start(){

        if(notices == null || notices.size() == 0){
            return false;
        }

        removeAllViews();

        for(int i=0; i< notices.size(); i++){
            final TextView textview = createTextView(notices.get(i), i);
            // 当前MarqueeView中添加TextView，
            // 所以这个自定义view的核心就是viewFlipper中带有滚动动画的切换textview的功能
            addView(textview);
        }

        //上面的工作完成，开始滚动
        if(notices.size() > 1){
            startFlipping();
        }

        return true;
    }

    private TextView createTextView(String text, int position){
        TextView tv = new TextView(mContext);
        tv.setGravity(gravity);
        tv.setText(text);
        tv.setTextSize(textSize);
        tv.setTextColor(textColor);
        tv.setSingleLine(singleLine);
        tv.setTag(position);
        return tv;
    }

    public List<String> getNotices() {
        return notices;
    }

    public void setNotices(List<String> notices) {
        this.notices = notices;
    }
}